package io.github.yotta.common.web.config;

import io.github.yotta.common.web.filter.FilterChainExceptionHandlerFilter;
import io.github.yotta.common.web.filter.HeaderContextFilter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;

import javax.servlet.Filter;

/**
 * @author yotta
 * @date 2023/8/4 14:05
 */
@Configuration
@AutoConfigureBefore({JacksonAutoConfiguration.class})
public class WebCommonAutoConfiguration {

    @Bean
    public FilterRegistrationBean<Filter> registrationHeaderContextFilterBean() {
        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new HeaderContextFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(HeaderContextFilter.ORDER);
        return registrationBean;
    }



    @Bean
    public FilterRegistrationBean<FilterChainExceptionHandlerFilter> filterChainExceptionHandlerBean(@Qualifier("handlerExceptionResolver") HandlerExceptionResolver resolver) {
        FilterChainExceptionHandlerFilter filterChainExceptionHandler = new FilterChainExceptionHandlerFilter(resolver);
        FilterRegistrationBean<FilterChainExceptionHandlerFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(filterChainExceptionHandler);
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(FilterChainExceptionHandlerFilter.ORDER);
        return registrationBean;
    }
}
